import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-11
 * Time: 11:07
 */
class MyThreadPool{
    //阻塞队列
   private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(1000);
   List<Thread> list=new ArrayList<>();
   int maxPoolSize=0;

    //初始化线程池,设置线程池的核心线程数
    public MyThreadPool(int corePoolSize, int maxPoolSize){
        this.maxPoolSize=maxPoolSize;
        for (int i = 0; i < corePoolSize; i++) {
            Thread t=new Thread(()->{
                Runnable runnable= null;
                try {
                    runnable = queue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runnable.run();
            });
            t.start();
            list.add(t);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        //线程数目扩容
        if(queue.size()>=500&&list.size()<=maxPoolSize){
            Thread t=new Thread(()->{
                Runnable runnable1= null;
                try {
                    runnable1 = queue.take();
                    runnable1.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            });
            t.start();
            list.add(t);
        }
        queue.put(runnable);
    }
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool= new MyThreadPool(10,20);
        for (int i = 0; i <10000 ; i++) {
            int id=i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello "+id+", "+Thread.currentThread().getName());
                }
            });
        }

    }
}
